package segmentation;

import java.util.Vector;

import textPixelId.ClusterInfo;
import utils.Utils;

public class CharInfo implements Comparable<CharInfo>{
	public ClusterInfo cluster;
	public int[][] charImage;

	public CharInfo(ClusterInfo c) {
		cluster = c;
		charImage = new int[c.r1-c.r0+1][c.c1-c.c0+1];
	}

	@Override
	public int compareTo(CharInfo o) {
		return cluster.c0 - o.cluster.c0;	// left to right
	}
	
	public String toString() {
		return cluster.toString();
	}
	
	static int gridH = 10;
	static int gridW = 10;	
	public static int getNumFeatures() {
		return gridW * gridH;
	}
	
	public double[] extractFeatures() {
		double[] v = new double[gridH * gridW];
		
		// count number of pixels in each cell
		int h = charImage.length;
		int w = charImage[0].length;
		
		Utils.showImage(Utils.arrayToImage(charImage), "character");
		
		double w0 = ((double)w) / gridW;
		double h0 = ((double)h) / gridH;
		for (int r=0; r<h; r++) {
			for (int c=0; c<w; c++) {
				if (charImage[r][c] == 0) { // black pixel
					int cell_r = (int)(r / h0);
					int cell_c = (int)(c / w0);
					if (cell_r == gridH) cell_r = gridH-1;
					if (cell_c == gridW) cell_c = gridW-1;
					v[cell_r * gridW + cell_c]++;
				}
			}
		}
		
		// normalize feature values
		for (int i=0; i<gridW * gridH; i++) {
			v[i] /= cluster.num_pixels;
			if (Double.isNaN(v[i])) v[i] = 0;
			if (Double.isInfinite(v[i])) v[i] = 0;
			
		}
		
/*		
		int[][] m = new int[100][100];
		for (int i=0; i<10; i++) {
			for (int j=0; j<10; j++) {
				int val = (int)(v[i*10+j]*30);
				
				System.out.println(i + " " + j + " " + v[i*10+j] + " " + val + " " + cluster.num_pixels);
				for (int k=0; k<10; k++) {
					for (int l=0; l<10; l++) {
						m[i*10+k][j*10+l] = val;
						if (m[i*10+k][j*10+l] > 255) m[i*10+k][j*10+l] = 255;
					}
				}
			}
		}
		
		System.out.println("I am here");
		Utils.showImage(Utils.arrayToImage(m), "feats");
*/			
			
		return v;
	}
	
}
